/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import './style.scss';
import commonService from '../../../common/commonService';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import service from './service';
import lib from '../../../common/lib';
import format from '../../../common/format';
import moment from 'moment';
import {PAGE_SIZE} from '../../../../grsc-base/common/constants';
import Pager from '../../../../grsc-base/component/Pager.vue';

export default Vue.extend({
    name: "guojing",
    template: tpl,
    components: {
        Loading,
        NoData,
        isLoading: true,
        Pager
    },

    data() {
        return {
            format,
            lib: lib,
            isLoading:true,
            binDing:false,
            isLoadings:false,
            isLoadingb:false,
            binDingUserName:'',
            binDingPassWord:'',
            list:[],
            amount:'',
            payPassword:'',
            pageNo: 1,
            pageSize: PAGE_SIZE,
            pageNum: 0,
        }
    },
    created() {
       this.seachAccount();
    },
    mounted(){
    },
    methods: {
        //翻页
        changePage(currentPage) {
            this.pageNo = currentPage;
            this.getTradeGlideList();
        },
        //格式化時間
        timezone(value, format = 'YYYY-MM-DD HH:mm:ss') {
            let currentTimezone = this.getCurrentTimezone();
            return moment(value).utc().utcOffset(-currentTimezone).format(format);
        },
        getCurrentTimezone() {
            let d = new Date();
            return d.getTimezoneOffset() / 60;
        },
        //查询列表
        getTradeGlideList(){
            service.getTradeGlideList({
                pageNo: this.pageNo,
                pageSize: PAGE_SIZE
            }).done((rep)=>{
                this.isLoading = false;
                this.list = rep.data.list;
                    this.pageNum = rep.data.pageNum;
            }).fail((rep)=>{
                this.isLoading = false;
                window.error(rep.msg)
            })
        },
        //转出
        Deposit(){
            if(!this.amount){
                alert({
                    content:this.$t('guojing.TransferNumberTip')
                })
                return false
            }else if(!Number(this.amount+'')){
                alert({
                    content:this.$t('guojing.TransferNumberTipE')
                })
                return false
            }
            if(!this.payPassword){
                alert({
                    content:this.$t('guojing.PayPasswordTip')
                })
                return false
            }
            let obj = {
                type:'APA',
                amount:this.amount
            }
            this.isLoadings = true
            service.Deposit(obj,{'pay-password':this.payPassword}).done((rep)=>{
                this.isLoadings = false;
                if(rep.code == '100200' && (rep.msg == '成功' || rep.msg == 'Success' || rep.msg == 'Éxito ') && rep.data == true){
                    this.amount = '';
                    this.payPassword = '';
                    window.success(rep.msg)
                    this.getTradeGlideList();
                }else if (rep.code === window.CODE.PAY_PWD.CODE && rep.data && rep.data.num) {
                    let num = rep.data.num;
                    if (3 - num > 0) {
                        window.error(this.$t('common.IncorrectAssetPassTip',{num:3-num}));
                    } else {
                        window.error(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                    }
                } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                    window.error(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                }else {
                    this.amount = '';
                    this.payPassword = '';
                    window.error(rep.msg)
                    this.getTradeGlideList();
                }
            }).fail((rep)=>{
                this.isLoadings = false;
                window.error(rep.msg)
            })
        },
        //查询是否有绑定
        seachAccount(){
            service.seachAccount().done((rep)=>{
                if(rep.code == '100200' && (rep.msg == '成功' || rep.msg == 'Success' || rep.msg == 'Éxito ') && rep.data != null){
                    this.binDing = true;
                    this.getTradeGlideList();
                }else{
                    this.binDing = false;
                }
            }).fail((rep)=>{
                window.error(rep.msg)
            })
        },
        //绑定
        binDingSubmit(){
            if(!this.binDingUserName){
                alert({
                    content: this.$t('guojing.bingdingUserTip')
                })
                return false;
            }
            if(!this.binDingPassWord){
                alert({
                    content: this.$t('guojing.binDingPassWordTip')
                })
                return false;
            }
            let obj = {
                userName:this.binDingUserName,
                passWord:this.binDingPassWord
            }
            this.isLoadingb = true;
            service.bindingAccount(obj).done((rep)=>{
                this.isLoadingb = false;
                if(rep.code == '100200' && (rep.msg == '成功' || rep.msg == 'Success' || rep.msg == 'Éxito ') && rep.data != null){
                    this.binDingUserName = '';
                    this.binDingPassWord = '';
                    this.binDing = true;
                    window.success(rep.msg)
                    this.getTradeGlideList();
                }else {
                    window.error(rep.msg)
                }
            }).fail((rep)=>{
                console.log(rep)
                this.isLoadingb = false;
                window.error(rep.msg)
            })
        },
    },
    watch: {}
});
